package com.jin.blog.controller.admin;

import java.util.Date;
import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.ehcache.CacheKit;
import com.jin.blog.interceptor.SessionInterceptor;
import com.jin.blog.pojo.Article;
import com.jin.blog.pojo.Project;

@Before(SessionInterceptor.class)
public class ArticleController extends Controller {
	private Article article;

	public void index() {
		Integer pageNum = getParaToInt("p", 1);
		Page<Article> page = Article.dao.paginate(pageNum, 10, "select *",
				"from article order by id desc");
		setAttr("page", page);
		render("articleList.html");
	}
	public void delete(){
		Article.dao.clear().set("id", getPara(0)).delete();
		CacheKit.removeAll("article");
		redirect("/admin/article/");
	}
	public void edit(){
		article = Article.dao.findById(getPara(0));
		setAttr("article",article);
		List<Project> projectList = Project.dao.find("select * from project");
		setAttr("projectList", projectList);
		render("addArticle.html");
	}
	
	public void editArticle(){
		article = getModel(Article.class);
		article.update();
		CacheKit.removeAll("article");
		setAttr("success", true);
		setAttr("msg", "提交成功");
		renderJson();
	}
	
	public void add() {
		List<Project> projectList = Project.dao.find("select * from project");
		setAttr("article","");
		setAttr("projectList", projectList);
		render("addArticle.html");
	}

	public void addAtricle() {
		article = getModel(Article.class);
		article.set(Article.CREATE_DATE_TIME,new Date());
		article.save();
		CacheKit.removeAll("article");
		setAttr("success", true);
		setAttr("msg", "提交成功");
		renderJson();
	}
	
}
